﻿using log4net;
using System.Data;
using System.Reflection;
using Koala.Pro.CodeGenerator.UI.DatabaseModel.Schema;

namespace Koala.Pro.CodeGenerator.UI.DatabaseModel.Data
{
    public abstract class Database : IListData
    {
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private string connectString;

        private string noSuffixOfTableName = string.Empty;

        public string ConnectString
        {
            get
            {
                return connectString;
            }
            set
            {
                connectString = value;
            }
        }

        public string NoSuffixOfTableName
        {
            get
            {
                return noSuffixOfTableName;
            }
            set
            {
                noSuffixOfTableName = value;
            }
        }

        public abstract IDictionary<string, DatabaseInfo> ListDatabases();

        public abstract IDictionary<string, TableInfo> ListTables(string database);

        public abstract IDictionary<string, ColumnInfo> ListColumns(string database, string tableName);

        public abstract IDictionary<string, ViewInfo> ListViews(string database);

        public abstract IDictionary<string, ProcedureInfo> ListProcedures(string database);

        protected string GetSimplyNetType(string netType)
        {
            string text = netType;
            return netType.ToLower() switch
            {
                "system.int16" => "short",
                "system.int32" => "int",
                "system.int64" => "long",
                "system.uint16" => "ushort",
                "system.uint32" => "uint",
                "system.uint64" => "ulong",
                "system.boolean" => "bool",
                "system.single" => "float",
                "system.datetime" => "DateTime",
                "system.guid" => "Guid",
                _ => netType.ToLower().Replace("system.", string.Empty),
            };
        }

        protected string GetControlType(string netType)
        {
            string text = netType;
            return netType.ToLower() switch
            {
                "system.int16"    => "numeric",
                "system.int32"    => "numeric",
                "system.int64"    => "numeric",
                "system.uint16"   => "numeric",
                "system.uint32"   => "numeric",
                "system.uint64"   => "numeric",
                "system.boolean"  => "numeric",
                "system.single"   => "numeric",
                "system.datetime" => "datetime",
                _                 => "text",
            };
        }

        public abstract DataSet ExecuteSql(string database, string query);

        public abstract bool TestConnection();
    }

}
